package ru.uskov.trainer.model.impl;


import java.math.BigDecimal;
import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import ru.uskov.trainer.datastore.impl.ContextGenerator;
import ru.uskov.trainer.model.IProductItem;
import ru.uskov.trainer.model.WrongArgumentException;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class ProductItem implements IProductItem {
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.UUIDSTRING)
  private String uniqueId;
  
  public String getId() {
    return uniqueId;
  }
  @Persistent
  private String userId;
  
  @Persistent
  private Date addDate;
  
  @Persistent
  private Long rootProductId;
  
  @NotPersistent
  private ProductDescription rootProduct;
  @Persistent
  private BigDecimal quantity;
  
  public ProductItem(ProductItem item) {
    this.addDate = item.addDate;
    this.quantity = item.quantity;
    this.rootProduct = item.rootProduct;
    this.rootProductId = item.rootProductId;
    this.uniqueId = item.uniqueId;
    this.userId = item.userId;
  }
  
  public ProductItem(String userId, ProductDescription description, String quantityDecription) throws WrongArgumentException {
    try {
      rootProduct = description;
      //rootDescription = null;// description.getDescription();//TODO
      this.userId = userId;
      addDate = new Date(System.currentTimeMillis());
      // in gramms
      quantity = (new BigDecimal(quantityDecription)).setScale(QUANTITY_ACC);
      rootProductId = description.getDBKey();
    } catch (NumberFormatException e) {
      throw new WrongArgumentException(quantityDecription + " is not correct quantity description");
    }
  }

  private void enshureDescription() {
    if (rootProduct == null) {
      rootProduct = ContextGenerator.getInstance().getProductDescriptionForId(rootProductId);      
    }
  }
  
  @Override
  public BigDecimal getCarbohydrates() {
    enshureDescription();
    return rootProduct.getCarbohydrates().multiply(quantity).setScale(CARBOHYD_ACC, BigDecimal.ROUND_HALF_UP);    
  }

  @Override
  public BigDecimal getProteins() {
    enshureDescription();
    return rootProduct.getProteins().multiply(quantity).setScale(PROTEIN_ACC, BigDecimal.ROUND_HALF_UP);
  }

  @Override
  public BigDecimal getFat() {
    enshureDescription();
    return rootProduct.getFat().multiply(quantity).setScale(FAT_ACC, BigDecimal.ROUND_HALF_UP);
  }

  @Override
  public BigDecimal getKKalls() {
    enshureDescription();
    return rootProduct.getKKalls().multiply(quantity).setScale(KKAL_ACC, BigDecimal.ROUND_HALF_UP);
  }

  @Override
  public BigDecimal getWater() {
    enshureDescription();
    return rootProduct.getWater().multiply(quantity).setScale(WATER_ACC, BigDecimal.ROUND_HALF_UP);
  }

  @Override
  public String getName() {
    enshureDescription();
    return rootProduct.getName();
  }
  
  @Override
  public BigDecimal getQuantity() {
    enshureDescription();
    return quantity;
  }

  
  @Override
  public String toString() {
    return "Product: " + quantity + " for product " + rootProduct;
  }
}
